package com.jjwxc.jwjskandriod.widget;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.SeekBar;
import android.widget.TextView;

import com.jjwxc.jwjskandriod.R;
import com.jjwxc.jwjskandriod.framework.utils.PreUtils;
import com.jjwxc.jwjskandriod.readActivity.utils.Constant;
import com.jjwxc.jwjskandriod.readActivity.view.PageLoader;
import com.jjwxc.jwjskandriod.readActivity.view.PageMode;

public class AutoPagePop {
    int barNum = 5;

    boolean isClose = false;

    boolean pageMode = true;

    private LinearLayout ll_one;
    private LinearLayout ll_two;

    private ImageView img_xuan_one;
    private ImageView img_xuan_two;
    private TextView txt_name_one;
    private TextView txt_name_two;
    private PageLoader mPageLoader;

    public void autoPopWindow(View mView, Activity context, PageLoader mPageLoader, AutoPageState autoPageState) {
        this.mPageLoader = mPageLoader;

        View view = LayoutInflater.from(context).inflate(R.layout.auto_page_pop, null, false);

        ImageView img_sub = view.findViewById(R.id.img_sub);
        ImageView img_add = view.findViewById(R.id.img_add);
        LinearLayout ll_close = view.findViewById(R.id.ll_close);
        LinearLayout ll_quxiao = view.findViewById(R.id.ll_quxiao);
        ll_one = view.findViewById(R.id.ll_one);
        img_xuan_one = view.findViewById(R.id.img_xuan_one);
        txt_name_one = view.findViewById(R.id.txt_name_one);


        ll_two = view.findViewById(R.id.ll_two);
        img_xuan_two = view.findViewById(R.id.img_xuan_two);
        txt_name_two = view.findViewById(R.id.txt_name_two);

        CircleSeekBar seek_bar = view.findViewById(R.id.seek_bar);
        //設置相關屬性值
        seek_bar.setDrawStyle(0);
        seek_bar.setCount(11);

        // 設置默認的進度
        int progress = PreUtils.getInt(Constant.Progress, -1);
        if (progress != -1) {
            seek_bar.setProgress(progress);
        }

        barNum = seek_bar.getProgress();
        //1.構造一個PopupWindow，參數依次是加載的 View，寬高
        final PopupWindow popWindow = new PopupWindow(view,
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        //設置加載動畫
        popWindow.setAnimationStyle(R.style.PopupWindow);
        //這些為了點擊非PopupWindow區域，PopupWindow會消失的，如果沒有下面的
        //代碼的話，你會發現，當你把PopupWindow顯示出來了，無論你按多少次後退鍵
        //PopupWindow並不會關閉，而且退不出程序，加上下述代碼可以解決這個問題

        popWindow.setTouchable(true);
        popWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
//                setBackgroundAlpha(1, context);
                double bar;

                if (pageMode) {
                    //進度條為100,
//                    bar = 20L * (100-barNum) / 100;
                    //進度條為100,
                    bar = 20D - ((barNum * 15D) / 100);
                    if (bar <= 0) {
                        bar = 20D;
                    }
                } else {
                    /*if (barNum > 100) {
                        barNum = 100;
                    }

                    bar = 2D - (barNum * 1.8D) / 100D;
                    if (bar <= 0) {
                        bar = 0.2D;
                    }*/
                    bar = 0.001;
                }
                autoPageState.onItemClick(bar, isClose);
            }
        });
        //默認翻頁方式  根據存儲來判斷
        String mode = PreUtils.getString(Constant.PageMode, PageMode.SIMULATION.name());
        if (PageMode.SIMULATION.name().equals(mode)) {
            selectStatus(0);
        } else {
            selectStatus(1);
        }

//        pageMode = true;
        popWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));    //要為popWindow設置一個背景才有效
        //設置 popupWindow 顯示的位置，參數依次是參照 View，x軸的偏移量，y軸的偏移量
        popWindow.showAtLocation(mView, Gravity.BOTTOM, 0, 0);

//        setBackgroundAlpha(0.3f, context);
        seek_bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (seekBar.getProgress() < 100) {
                    seekBar.setProgress(Math.round(seekBar.getProgress() / 10) * 10 + 5);
                } else {
                    seekBar.setProgress(105);
                }
                barNum = progress;
                PreUtils.setInt(Constant.Progress, seek_bar.getProgress());
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });
        img_add.setOnClickListener(v -> {
            barNum = barNum + 10;
            if (barNum < 100) {
                seek_bar.setProgress(Math.round(barNum / 10) * 10 + 5);
            } else {
                barNum = 100;
                seek_bar.setProgress(105);
            }

            PreUtils.setInt(Constant.Progress, seek_bar.getProgress());
        });
        img_sub.setOnClickListener(v -> {
            barNum = barNum - 10;
            if (barNum < 0) {
                seek_bar.setProgress(5);
                barNum = 0;
            } else {
                seek_bar.setProgress(Math.round(barNum / 10) * 10 + 5);
            }

            PreUtils.setInt(Constant.Progress, seek_bar.getProgress());
        });
        ll_one.setOnClickListener(v -> {
            /*ll_one.setBackgroundResource(R.drawable.tag_screen_select_true);
            ll_two.setBackgroundResource(R.drawable.tag_screen_select_false);
            img_xuan_one.setVisibility(View.VISIBLE);
            img_xuan_two.setVisibility(View.GONE);
            txt_name_one.setTextColor(Color.parseColor("#4D998D"));
            txt_name_two.setTextColor(Color.parseColor("#333333"));
            mPageLoader.setPageMode(PageMode.SIMULATION);
            PreUtils.setString(Constant.PageMode, PageMode.SIMULATION.name());

            pageMode = true;*/

            selectStatus(0);
        });
        ll_two.setOnClickListener(v -> {
            /*ll_two.setBackgroundResource(R.drawable.tag_screen_select_true);
            ll_one.setBackgroundResource(R.drawable.tag_screen_select_false);
            img_xuan_two.setVisibility(View.VISIBLE);
            img_xuan_one.setVisibility(View.GONE);
            txt_name_two.setTextColor(Color.parseColor("#4D998D"));
            txt_name_one.setTextColor(Color.parseColor("#333333"));
            mPageLoader.setPageMode(PageMode.SCROLL);
            PreUtils.setString(Constant.PageMode, PageMode.SCROLL.name());

            pageMode = false;*/

            selectStatus(1);
        });
        ll_close.setOnClickListener(v -> {
            isClose = true;
            popWindow.dismiss();
        });
        ll_quxiao.setOnClickListener(v -> popWindow.dismiss());
    }

    private void selectStatus(int type) {
        if (type == 0) {
            ll_one.setBackgroundResource(R.drawable.tag_screen_select_true);
            ll_two.setBackgroundResource(R.drawable.tag_screen_select_false);
            img_xuan_one.setVisibility(View.VISIBLE);
            img_xuan_two.setVisibility(View.GONE);
            txt_name_one.setTextColor(Color.parseColor("#4D998D"));
            txt_name_two.setTextColor(Color.parseColor("#333333"));
            mPageLoader.setPageMode(PageMode.SIMULATION);
            PreUtils.setString(Constant.PageMode, PageMode.SIMULATION.name());

            pageMode = true;
        } else if(type == 1) {
            ll_two.setBackgroundResource(R.drawable.tag_screen_select_true);
            ll_one.setBackgroundResource(R.drawable.tag_screen_select_false);
            img_xuan_two.setVisibility(View.VISIBLE);
            img_xuan_one.setVisibility(View.GONE);
            txt_name_two.setTextColor(Color.parseColor("#4D998D"));
            txt_name_one.setTextColor(Color.parseColor("#333333"));
            mPageLoader.setPageMode(PageMode.SCROLL);
            PreUtils.setString(Constant.PageMode, PageMode.SCROLL.name());

            pageMode = false;
        }
    }

    public interface AutoPageState {
        void onItemClick(double speed, boolean isClose);
    }

    public void setBackgroundAlpha(float alpha, Activity activity) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.dimAmount = 0F;
        activity.getWindow().setAttributes(lp);
    }
}